from django.conf import settings
import uuid
import os



def user_dir_path(instance, filename):
    '''
    图片/文件存储路径
    :param instance: 实例
    :param filename: str, 含有数据格式文件名
    :return: str
    '''
    *name, ext = filename.split('.')
    filename = '{name}_{org}.{ft}'.format(name=uuid.uuid4().hex, org=".".join(name), ft=ext)
    return f"team/{filename}"

def get_file_path(instance, filename):
    return f"{os.path.join(settings.MEDIA_URL, 'supermarket')}/{filename}"

def get_base_dir():
    return f"{os.path.join(settings.MEDIA_URL, 'supermarket')}"

def set_file_path(instance, filename):
    '''存储路径'''
    return "{absdir}/supermarket/{username}/{title}/{filename}".format(
        absdir=os.path.join(settings.MEDIA_URL, f"{instance.user.id}"),
        username=str(instance.user.id).strip(),
        title=str(instance.name.name).strip(),
        filename=filename
    )